package com.ruoyi.user.controller;

import java.util.Date;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.system.api.model.LoginUser;
import com.ruoyi.user.domain.AppRoleMenu;
import com.ruoyi.user.domain.AppUserRole;
import com.ruoyi.user.service.AppRoleMenuService;
import com.ruoyi.user.service.AppUserRoleService;
import com.ruoyi.user.service.IUserService;
import com.ruoyi.user.service.RemoteSysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.api.domain.User;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户(user)Controller
 * 
 * @author fly
 * @date 2024-09-12
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    @Autowired
    private AppRoleMenuService appRoleMenuService;


    @Autowired
    private AppUserRoleService appUserRoleService;

    @Autowired
    private RemoteSysUserService remoteSysUserService;

    /**
     * 查询用户(user)列表
     */
//    @RequiresPermissions("user:user:list")
    @GetMapping("/list")
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户(user)列表
     */
    @RequiresPermissions("user:user:export")
    @Log(title = "用户(user)", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户(user)数据");
    }

    /**
     * 获取用户(user)详细信息
     */
    @RequiresPermissions("user:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户(user)
     */
    @RequiresPermissions("user:user:add")
    @Log(title = "用户(user)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user)
    {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 修改用户(user)
     */
    @RequiresPermissions("user:user:edit")
    @Log(title = "用户(user)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户(user)
     */
    @RequiresPermissions("user:user:remove")
    @Log(title = "用户(user)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }

    @InnerAuth
    @GetMapping("/info/{phone}")
    public R<LoginUser> info(@PathVariable("phone") String phone)
    {
        com.ruoyi.system.api.domain.User user = userService.selectUserInfo(phone);
        if (StringUtils.isNull(user))
        {
            user = new User();
            user.setPhone(phone);
            user.setRegTime(new Date());
            userService.insertUser(user);
            appUserRoleService.addAppUserRole(new AppUserRole(user.getId(),2));
        }
        // 角色集合
        AppUserRole appUserRole = new AppUserRole();
        appUserRole.setAppUserId(user.getId());
        Set<String> roles = appUserRoleService.getAppUserRole(appUserRole);
        // 权限集合
        List<Integer> roleId = appUserRoleService.getRoleId(user.getId());
        AppRoleMenu appRoleMenu = new AppRoleMenu();
        Set<String> permissions = appRoleMenuService.getAppRoleMenu(roleId.get(0));
        LoginUser UserVo = new LoginUser();
        UserVo.setUser(user);
        UserVo.setRoles(roles);
        UserVo.setPermissions(permissions);
        return R.ok(UserVo);
    }
}
